package com.ys.otms.utils;


import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

    public static final String DATE_FORMAT_1 = "yyyy-MM-dd";

    public static final String DATE_FORMAT_2 = "yyyyMMdd";

    public static final String DATETIME_FORMAT_1 = "yyyy-MM-dd HH:mm:ss";

    public static final String DATETIME_FORMAT_3 = "yyyy-MM-dd HH-mm-ss";

    public static final String DATETIME_FORMAT_2 = "yyyyMMddHHmmss";

    public static final String DATETIME_FORMAT_4 = "yyyy/MM/dd HH:mm:ss";

    public static final String DATE_FORMAT_3 = "yyyy年MM月dd日";


    public static Timestamp strFormatTimetamp(String str){
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        format.setLenient(false);
        Timestamp ts = null;
        try {
            ts = new Timestamp(format.parse(str).getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return ts;
    }



    public static Timestamp strFormatTimetampTest(String str){
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        format.setLenient(false);
        Timestamp ts = null;
        try {
            ts = new Timestamp(format.parse(str).getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return ts;
    }

    /**
     * 生成当前日期时间串
     * @return
     * @throws Exception
     */
    public static String getCurrentDateStr(){
        Date date=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddhhmmssSSS");
        return sdf.format(date);
    }

    public static SimpleDateFormat getDateFormat(String format) {
        return new SimpleDateFormat(format);
    }

    public static String date2String(Date date, String format) {
        if (date == null) {
            return "";
        }
        SimpleDateFormat sdf = getDateFormat(format);
        return sdf.format(date);
    }

    public static String date2String(Date date) {
        return date2String(date, DATE_FORMAT_1);
    }

    public static String getCurrentDate() {
        Date date = new Date();
        return date2String(date, DATE_FORMAT_1);
    }

    public static Timestamp dateOfCalculation(Timestamp timestamp, int consumeTypeId){
        Calendar c = Calendar.getInstance();//获得一个日历的实例
        SimpleDateFormat sdf = getDateFormat(DATETIME_FORMAT_1);
        Date date = null;
        try{
            date = sdf.parse(timestamp.toString());//初始日期
        }catch(Exception e){
        }
        c.setTime(date);//设置日历时间
        if(consumeTypeId == 2){
            c.add(Calendar.MONTH,1);//在日历的月份上增加6个月
        }else if(consumeTypeId==3){
            c.add(Calendar.YEAR,1);//在日历的月份上增加6个月
        }
        Timestamp t =  Timestamp.valueOf(sdf.format(c.getTime()));
        return t;
    }

    public static Timestamp dateOfCalculation(Date date){
        Calendar c = Calendar.getInstance();//获得一个日历的实例
        SimpleDateFormat sdf = getDateFormat(DATETIME_FORMAT_1);
        c.setTime(date);
        c.add(Calendar.MONTH,-1);
        Timestamp t = Timestamp.valueOf(sdf.format(c.getTime()));
        System.out.println(t);
        return t;
    }


    public static Timestamp getSeePhoneTime(Timestamp timestamp,int day){
        Calendar c = Calendar.getInstance();//获得一个日历的实例
        SimpleDateFormat sdf = getDateFormat(DATETIME_FORMAT_1);
        Date date = null;
        try{
            date = sdf.parse(timestamp.toString());//初始日期
        }catch(Exception e){
        }
        c.setTime(date);//设置日历时间
        c.add(Calendar.DATE,day);//在日历的月份上增加6个月
        Timestamp t =  Timestamp.valueOf(sdf.format(c.getTime()));
        return t;
    }

    public static Timestamp getSeePhoneStr(String str,int day){
        Calendar c = Calendar.getInstance();//获得一个日历的实例
        SimpleDateFormat sdf = getDateFormat(DATETIME_FORMAT_1);
        Date date = null;
        try{
            date = sdf.parse(str);//初始日期
        }catch(Exception e){
        }
        c.setTime(date);//设置日历时间
        c.add(Calendar.DATE,day);//在日历的月份上增加6个月
        Timestamp t =  Timestamp.valueOf(sdf.format(c.getTime()));
        return t;
    }


    public static Date string2Date(String date) {
        Date ret = null;
        if (date == null || date.length() == 0) {
            return ret;
        }
        if (date.length() > 11) {
            if (date.indexOf('-') > 0) {
                if (date.indexOf(':') > 0) {
                    ret = string2Date(date, DATETIME_FORMAT_1);
                }
                else {
                    ret = string2Date(date, DATETIME_FORMAT_3);
                }
            }
            else if (date.indexOf('/') > 0) {
                ret = string2Date(date, DATETIME_FORMAT_4);
            }
            else {
                ret = string2Date(date, DATETIME_FORMAT_2);
            }
        }
        else {
            if (date.indexOf('-') > 0) {
                ret = string2Date(date, DATE_FORMAT_1);
            }
            else if (date.length() == 8) {
                ret = string2Date(date, DATE_FORMAT_2);
            }
            else {
                ret = string2Date(date, DATE_FORMAT_3);
            }
        }
        return ret;
    }

    public static Date string2Date(String date, String format) {
        if (StringUtil.isEmpty(format)) {
            throw new IllegalArgumentException("the date format string is null!");
        }
        SimpleDateFormat sdf = getDateFormat(format);
        if (null == sdf) {
            throw new IllegalArgumentException("the date format string is not matching available format object");
        }
        try {
            return sdf.parse(date);
        }
        catch (ParseException e) {
            throw new IllegalArgumentException("the date string " + date + " is not matching format: " + format, e);
        }
    }

    /**
     * yyyy-MM-dd HH:mm:ss
     *
     * @return
     */
    public static String getStandardNowTime() {
        SimpleDateFormat sdf = getDateFormat(DATETIME_FORMAT_1);
        return sdf.format(new Date());
    }

    /**
     * yyyy-MM-dd HH:mm:ss
     *
     * @return
     */
    public static String getStandardTime(long timeStamp) {
        SimpleDateFormat sdf = getDateFormat(DATETIME_FORMAT_1);
        return sdf.format(new Date(timeStamp));
    }

    public static boolean isInRange(Date date, Date beginDate, Date endDate) {
        long time = date.getTime();
        long beginTime = beginDate.getTime();
        long endTime = endDate.getTime();

        return time >= beginTime && time <= endTime;
    }


    public static int compare(Date beginDate, Date endDate) {
        int ret = 1;
        long beginTime = beginDate.getTime();
        long endTime = endDate.getTime();

        if (beginTime > endTime) {
            ret = 2;
        }
        if (beginTime == endTime) {
            ret = 1;
        }
        if (beginTime < endTime) {
            ret = 0;
        }
        return ret;
    }

    public static Date offsetWeek(Date date, int weeks) {
        return offsetDay(date, 7 * weeks);
    }


    public static java.sql.Date getLastDayOfMonth(java.sql.Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        calendar.set(Calendar.DAY_OF_MONTH, maxDay);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        date.setTime(calendar.getTimeInMillis());
        return date;
    }

    public static Date offsetDay(Date date, int days) {
        return offsetHour(date, 24 * days);
    }

    public static Date offsetHour(Date date, long hours) {
        return offsetMinute(date, 60 * hours);
    }

    public static Date offsetMinute(Date date, long minutes) {
        return offsetSecond(date, 60 * minutes);
    }

    public static Date offsetSecond(Date date, long seconds) {
        if (date == null) {
            return null;
        }
        long time = date.getTime();
        long time2 = time + (seconds * 1000);
        long time3 = time + (seconds * 1000) - 60 * 60 * 1000;
        java.sql.Date date2 = new java.sql.Date(time2);
        java.sql.Date date3 = new java.sql.Date(time3);

        Calendar calendarDate = Calendar.getInstance();
        calendarDate.setTime(date);
        Calendar calendarDate2 = Calendar.getInstance();
        calendarDate2.setTime(date2);
        Calendar calendarDate3 = Calendar.getInstance();
        calendarDate3.setTime(date3);

        long dstDate = calendarDate.get(Calendar.DST_OFFSET);
        long dstDate2 = calendarDate2.get(Calendar.DST_OFFSET);
        long dstDate3 = calendarDate3.get(Calendar.DST_OFFSET);

        long dstOffset = dstDate - dstDate2;
        if (dstOffset == 0 || (dstDate2 - dstDate3 != 0 && dstDate2 != 0)) {
            return date2;
        }
        else {
            return new java.sql.Date(time2 + dstOffset);
        }
    }

    /**
     * beginTime>endTime返回2,beginTime=endTime返回1,beginTime<endTime返回0
     * @param beginTime
     * @param endTime
     * @return
     */
    public static int compareToTime(String beginTime, String endTime) {
        if (StringUtil.isEmpty(beginTime) && StringUtil.isEmpty(endTime)) {
            return 1;
        }
        if (StringUtil.isEmpty(beginTime)) {
            return 0;
        }
        if (StringUtil.isEmpty(endTime)) {
            return 2;
        }
        return DateUtil.compare(DateUtil.string2Date(beginTime,"yyyy-MM-dd HH:mm:ss"),
                DateUtil.string2Date(endTime,"yyyy-MM-dd HH:mm:ss"));
    }

    /**
     * 毫秒转天时分秒
     * @param mss 毫秒数
     * @return <br/>
     */
    public static String formatDuring(long mss) {
        long ms = mss/1000;
        long days = ms / (60 * 60 * 24);
        long hours = (ms % (60 * 60 * 24)) / (60 * 60);
        long minutes = (ms % (60 * 60)) / (60);
        long seconds = ms % (60);
        if (0 != days) {
            return days + "天" + hours + "时" + minutes + "分" + seconds + "秒";
        }
        else if (0 != hours) {
            return hours + "时" + minutes + "分"
                    + seconds + "秒";
        }
        else if(0 != minutes) {
            return minutes + "分"
                    + seconds + "秒";
        }
        else if(0 != seconds){
            return seconds + "秒";
        }
        else {
            return "0";
        }
    }
}